﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace GTA
{
    public partial class FrmProfilerManagement : BaseForm
    {
        #region Starting Form
        public FrmProfilerManagement()
        {
            InitializeComponent();
        }
        private void FrmProfilerManagement_Load(object sender, EventArgs e)
        {
            StartForm();
        }
        private void StartForm()
        {
            try
            {
                LoadFunctionalities();
                SetProfilerFunctionalities();
            }
            catch (Exception ex)
            {
                HandleException("Inicializar a tela.", ex);
            }
        }
        #endregion

        #region Events
        private void cmdSave_Click(object sender, EventArgs e)
        {
            SaveProfilerFunctionalities();
        }
        private void lstFunctionalities_ColumnClick(object sender, ColumnClickEventArgs e)
        {
            SortList(lstFunctionalities, e.Column == 0 ? OrdenacaoListView.Tipo.Numerica : OrdenacaoListView.Tipo.Alfabetica, ref columnOrder, ref sortOrder, e.Column);
        }
        private void lstFunctionalities_Resize(object sender, EventArgs e)
        {
            lstFunctionalities.Columns[1].Width = lstFunctionalities.Width - lstFunctionalities.Columns[0].Width - 22;
        }
        private void opProfilerMaster_CheckedChanged(object sender, EventArgs e)
        {
            if (opProfilerMaster.Checked)
                SetProfilerFunctionalities();
        }
        private void opAdministrator_CheckedChanged(object sender, EventArgs e)
        {
            if (opProfilerAdministrator.Checked)
                SetProfilerFunctionalities();
        }
        private void opProfilerUser_CheckedChanged(object sender, EventArgs e)
        {
            if (opProfilerUser.Checked)
                SetProfilerFunctionalities();
        }
        #endregion

        #region Methods
        private void SaveProfilerFunctionalities()
        {
            try
            {
                if (ValidateFunctionalities())
                {
                    BusinessLogic.GTA bl = new BusinessLogic.GTA();
                    List<BusinessEntity.UserManagement.Functionality> functinoalities = GetSelectedFunctionalities();
                    if (opProfilerMaster.Checked)
                    {
                        bl.SaveProfilerFunctionalities(functinoalities, BusinessEntity.UserManagement.Profiler.Master);
                    }
                    else if (opProfilerAdministrator.Checked)
                    {
                        bl.SaveProfilerFunctionalities(functinoalities, BusinessEntity.UserManagement.Profiler.External);
                    }
                    else if (opProfilerUser.Checked)
                    {
                        bl.SaveProfilerFunctionalities(functinoalities, BusinessEntity.UserManagement.Profiler.User);
                    }
                    MessageBox.Show("Funcionalidades salvas com sucesso para o perfil!", "Confirmação", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                SetProfilerFunctionalities();
            }
            catch (Exception ex)
            {
                HandleException("Salvar as permissões do perfil de acesso.", ex);
            }
        }
        private List<GTA.BusinessEntity.UserManagement.Functionality> GetSelectedFunctionalities()
        {
            List<GTA.BusinessEntity.UserManagement.Functionality> list = new List<GTA.BusinessEntity.UserManagement.Functionality>();
            foreach (ListViewItem item in lstFunctionalities.CheckedItems)
                list.Add(new GTA.BusinessEntity.UserManagement.Functionality()
                {
                    ID = Convert.ToInt32(item.SubItems[0].Text),
                    Description = item.SubItems[1].Text
                });
            return list;
        }
        private bool ValidateFunctionalities()
        {
            if (lstFunctionalities.CheckedItems.Count == 0)
            {
                MessageBox.Show("Selecione ao menos uma funcionalidade para salvar!", "Alerta", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                return false;
            }
            else
                return true;
        }
        private void LoadFunctionalities()
        {
            try
            {
                List<BusinessEntity.UserManagement.Functionality> functionalities = new BusinessLogic.GTA().ListProfilerFunctionalities(null);
                lstFunctionalities.Items.Clear();
                if (functionalities != null && functionalities.Count > 0)
                {
                    foreach (BusinessEntity.UserManagement.Functionality item in functionalities)
                    {
                        ListViewItem listItem = new ListViewItem(item.ID.ToString());
                        listItem.SubItems.Add(item.Description);
                        lstFunctionalities.Items.Add(listItem);
                    }
                }
            }
            catch (Exception ex)
            {
                HandleException("Carregar a lista de funcionalidades", ex);
            }
        }
        private void ClearCheckedItems(ListView list)
        {
            foreach (ListViewItem item in list.Items)
                item.Checked = false;
        }
        private void SetProfilerFunctionalities()
        {
            try
            {
                List<BusinessEntity.UserManagement.Functionality> functionalities = null;
                BusinessLogic.GTA bl = new BusinessLogic.GTA();
                if (opProfilerMaster.Checked)
                {
                    functionalities = bl.ListProfilerFunctionalities(BusinessEntity.UserManagement.Profiler.Master);
                }
                else if (opProfilerAdministrator.Checked)
                {
                    functionalities = bl.ListProfilerFunctionalities(BusinessEntity.UserManagement.Profiler.External);
                }
                else if (opProfilerUser.Checked)
                {
                    functionalities = bl.ListProfilerFunctionalities(BusinessEntity.UserManagement.Profiler.User);
                }
                ClearCheckedItems(lstFunctionalities);
                if (functionalities != null && functionalities.Count > 0)
                {
                    foreach (ListViewItem item in lstFunctionalities.Items)
                    {
                        if (functionalities.Exists(it => it.ID.CompareTo(Convert.ToInt32(item.SubItems[0].Text)) == 0))
                            item.Checked = true;
                    }
                }
            }
            catch (Exception ex)
            {
                HandleException("Carregar as funcionalidades do perfil de acesso.", ex);
            }
        }
        #endregion
    }
}
